package com.liu.community.controller;/*
 *@Author:Liu Xing-zhi
 *Description:
 *Date:Crated in 15:07 2021/10/12
 */

import com.liu.community.dto.CommentCreateDto;
import com.liu.community.dto.CommentDto;
import com.liu.community.dto.QuestionDto;
import com.liu.community.dto.ResponseDto;
import com.liu.community.enums.CommentTypeEnum;
import com.liu.community.exception.ErrorCode;
import com.liu.community.pojor.Comment;
import com.liu.community.pojor.User;
import com.liu.community.server.BlogService;
import com.liu.community.server.CommentService;
import com.liu.community.server.QuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private BlogService blogService;

    @ResponseBody
    @RequestMapping(value = "/comment",method = RequestMethod.POST)
    public  Object post(@RequestBody CommentCreateDto commentDto,
                        HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        if (user == null){
           return ResponseDto.errorOF(ErrorCode.NOT_LOGIN);
        }
        //评论内容不能为空校验
        if(commentDto == null || StringUtils.isBlank(commentDto.getContent())){
            return ResponseDto.errorOF(ErrorCode.CONTENT_IS_EMPTY);
        }
        Comment comment = new Comment();
        comment.setContent(commentDto.getContent());
        comment.setType(commentDto.getType());
        comment.setParentId(commentDto.getParentId());
        comment.setGmtCreate(System.currentTimeMillis());
        comment.setGmtModified(comment.getGmtCreate());
        comment.setCommenter(user.getId());
        comment.setLikeCount(0);
        comment.setCommentCount(1);
        commentService.insert(comment,user);
        //响应结果信息
        return ResponseDto.loginOK();
    }
    @ResponseBody
    @RequestMapping(value = "/comment/comment",method = RequestMethod.POST)
    public  Object postIt(@RequestBody CommentCreateDto commentDto,
                        HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        if (user == null){
            return ResponseDto.errorOF(ErrorCode.NOT_LOGIN);
        }
        //评论内容不能为空校验
        if(commentDto == null || StringUtils.isBlank(commentDto.getContent())){
            return ResponseDto.errorOF(ErrorCode.CONTENT_IS_EMPTY);
        }
        Comment comment = new Comment();
        comment.setContent(commentDto.getContent());
        comment.setType(commentDto.getType());
        comment.setParentId(commentDto.getParentId());
        comment.setGmtCreate(System.currentTimeMillis());
        comment.setGmtModified(comment.getGmtCreate());
        comment.setCommenter(user.getId());
        comment.setLikeCount(0);
        comment.setCommentCount(1);
        commentService.insert(comment,user);
        //响应结果信息
        return ResponseDto.loginOK();
    }

    @ResponseBody
    @RequestMapping(value = "/commentAgain/{id}",method = RequestMethod.GET)
    public ResponseDto<List<CommentDto>> commentAgain(@PathVariable(name = "id")Integer id){
        List<CommentDto> commentDtoList = commentService.queryQuestionCommentByQuestionIdAndType(id);
        return  ResponseDto.okOf(commentDtoList);
    }

    @ResponseBody
    @RequestMapping(value = "/blogCommentAgain/{id}",method = RequestMethod.GET)
    public ResponseDto<List<CommentDto>> blogCommentAgain(@PathVariable(name = "id")Integer id){
        List<CommentDto> commentDtoList = commentService.queryBlogCommentByBlogAndType(id);
        return  ResponseDto.okOf(commentDtoList);
    }

    @GetMapping("/comment/{id}")
    public String goQuestion(@PathVariable(name = "id")Integer id, Model model){
        //根据id查询问题id
        Comment comment = commentService.queryCommentByCommentId(id);
        QuestionDto questionDto = questionService.queryQuestionById(comment.getParentId());
        List<QuestionDto> questionDtoRelated = questionService.queryRelated(questionDto);
        List<CommentDto> commentDtoList = commentService.queryQuestionCommentByQuestionIdAndType(id);//根據评论id查詢评论回复
        //增加阅读数
        questionService.increViewCount(comment.getParentId());
        model.addAttribute("question",questionDto);
        model.addAttribute("comments",commentDtoList);
        model.addAttribute("relatedQuestion",questionDtoRelated);

        return  "question";
    }


}
